<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="85px">
      <el-form-item label="关键词查询" prop="keyName">
        <el-input v-model="queryParams.keyName" placeholder="名称/助记符/备注" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="类型" prop="typeName">
        <el-select v-model="queryParams.typeName" placeholder="全部" clearable style="width: 240px">
          <el-option value="" label="全部"></el-option>
          <el-option v-for="item in typeList" :key="item.id" :label="item.typeName" :value="item.typeName" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown trigger="click">
          <el-button type="primary" size="mini">
            导出<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleExport('now')">导出当前</el-dropdown-item>
            <el-dropdown-item @click.native="handleExport('all')">导出全部</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>
    <el-table :row-class-name="rowStyle" highlight-current-row border v-loading="loading" :data="enumerationList"
      @selection-change="handleSelectionChange" height="calc(100vh - 250px)" :header-cell-style="{
        background: '#ebebeb'
      }">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <af-table-column label="类型" align="center" prop="type" />
      <af-table-column label="类型名称" align="center" prop="typeName" />
      <af-table-column label="名称" align="center" prop="name" />
      <af-table-column label="值" align="center" prop="numberValue" />
      <af-table-column label="助记符" align="center" prop="mnemonic" />
      <af-table-column label="顺序号" align="center" prop="sort" />
      <af-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"  align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']">编辑
          </el-button>
          <el-button v-if="(scope.row.name != '作废原因' && scope.row.name != '迟发原因' && scope.row.name != '修改原因')"
            size="mini" type="text" icon="el-icon-document-copy" @click="handleCopy(scope.row)"
            v-hasPermi="['system:dict:edit']">复制
          </el-button>
          <el-button size="mini" style="color: #FA5555;" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <pagination   style="margin:0;padding: 0;" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="值" prop="numberValue">
          <el-input v-model="form.numberValue" placeholder="请输入值" />
        </el-form-item>
        <el-form-item label="助记符" prop="mnemonic">
          <el-input v-model="form.mnemonic" placeholder="请输入助记符" />
        </el-form-item>
        <el-form-item label="顺序号" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入顺序号" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  enumerate,
  enumerateType,
  enumerateAdd,
  enumerateEdit,
  enumerateUpdate,
  enumerateDel,
  enumerateExport
} from '@/api/dictionary/systemSetting/enumeration'
import { nucleateType } from '@/api/dictionary/systemDictionary/nucleateType'

export default {
  name: 'enumeration',
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 枚举表格数据
      enumerationList: [],
      //枚举类型
      typeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        keyName: undefined,
        typeName: '',
        type: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: '请输入类型', trigger: 'blur' }
        ],
        typeName: [
          { required: true, message: '请输入类型名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        numberValue: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          // { pattern: /^(-)?[0-9]+([.][0-9]{1,})?$/, message: '请输入数字' }
          // { pattern: /\D/g, message: '请输入字符串' }
        ]
      }
    }
  },
  created () {
    this.getList()
    this.getTypeList()
  },
  methods: {
    /** 查询枚举列表 */
    getList () {
      this.loading = true
      enumerate(this.queryParams).then((response) => {
        this.enumerationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getTypeList () {
      enumerateType().then((res) => {
        this.typeList = res.rows
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        keyName: undefined,
        type: undefined,
        typeName: undefined
      }
      this.resetForm('form')
    },
    /** 查询按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '枚举设置：新增'
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const Id = row.id || this.ids
      enumerateEdit(Id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '枚举设置：编辑'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            enumerateUpdate(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            enumerateAdd(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const id = row.id || this.ids
      this.$confirm(
        '是否确认删除类型名称为"' + row.typeName + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function () {
          return enumerateDel(id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 复制按钮操作 */
    handleCopy (row) {
      this.$confirm(
        '是否确认复制该条数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function () {
          row.id = null
          return enumerateAdd(row)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('复制成功')
        })
    },

    // /** 导出按钮操作 */
    handleExport (e) {
      var different = ''
      var params = ''
      if (e === 'now') {
        different = '当前'
        params = this.queryParams //对应接口参数
      } else if (e === 'all') {
        different = '所有'
        params = null
      } else {
        return
      }
      let that = this
      let loading
      this.$confirm('是否确认导出' + different + '数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        loading = that.$loading({
          lock: true,
          text: '导出中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        enumerate(params).then((response) => {
          let mydata = [
            [
              '序号',
              '类型',
              '类型名称',
              '名称',
              '值',
              '助记符',
              '顺序号',
              '备注'
            ],
            ...response.rows.map((item, index) => ([
              index + 1,
              item.type,
              item.typeName,
              item.name,
              item.numberValue,
              item.mnemonic,
              item.sort,
              item.remark
            ]))
          ]
          let requireMerge = []
          that.$util.createxlsx({
            mydata,
            fileName: '枚举设置',
            dataList: response.rows,
            type: 'orderId',
            requireMerge,
            loading
          })
        })
      }).catch(function () {
      })
    },
    rowStyle ({ row }) {
      var arr = this.ids
      for (let i = 0; i < arr.length; i++) {
        if (row.id === arr[i]) {
          return 'rowStyle'
        }
      }
    }
  }
}
</script>
<style scoped>
.app-container {
  height: 85vh;
}

::v-deep .el-table__body tr.current-row>td {

  background: #a8d6fe !important;
}

::v-deep .rowStyle {
  background-color: #a8d6fe !important;
}

::v-deep .el-table__body tr:hover>td {
  background: #a8d6fe !important;
}
</style>
